<?php
/**
 * Created by PhpStorm.
 * User: lizx
 * Date: 2018/3/16
 * Time: 15:00
 */

namespace app\api\service;

use app\common\Service;
use think\Config;

class BaiduMapService extends Service
{

    public function __construct()
    {
        $this->ak = Config::get('baidu_map.ak');
    }

    //百度ak
    private $ak;

    /**
     * 根据经纬度获取地址信息
     * @param $lat
     * @param $lng
     * @return mixed
     */
    public function getSiteInfo($lat,$lng)
    {
        $location = "$lat,$lng";
        $str = "http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location={$location}&output=json&pois=1&pois=0&ak=$this->ak";

        $rtn = file_get_contents($str);

        $info = str_replace('renderReverse&&renderReverse(','',$rtn);
        $info = str_replace(')','',$info);

        $info = json_decode($info);
        $site_info['city'] = $info->result->addressComponent->city;
        $site_info['sematic_description'] = $info->result->sematic_description;
        $site_info['formatted_address'] = $info->result->formatted_address;

        return $site_info;
    }

    /**
     * 根据经纬度获取区域id
     * @param $lat
     * @param $lng
     * @return mixed
     */
    public function getSiteCode($lat,$lng)
    {
        $location = "$lat,$lng";
        $str = "http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location={$location}&output=json&pois=1&pois=0&ak=$this->ak";

        $rtn = file_get_contents($str);

        $info = str_replace('renderReverse&&renderReverse(','',$rtn);
        $info = str_replace(')','',$info);

        $info = json_decode($info);

        $site_info['adcode'] = $info->result->addressComponent->adcode;

        return $site_info;
    }

    /**
     * 计算两点距离及预计时间 按照骑行计算
     * @param $s_lat
     * @param $s_lng
     * @param $e_lat
     * @param $e_lng
     * @return mixed
     */
    public function getDistance($s_lat,$s_lng,$e_lat,$e_lng)
    {
        $str = "http://api.map.baidu.com/routematrix/v2/driving?output=json&origins={$s_lat},{$s_lng}&destinations={$e_lat},{$e_lng}&ak=$this->ak";
        $rt = json_decode(file_get_contents($str));

        $distance['distance'] = 0;
        $distance['remaining_time'] = 0;
        if($rt->status == 0){
            $distance['distance'] = $rt->result[0]->distance->value ?? 0;
            $distance['remaining_time'] = $rt->result[0]->duration->value ?? 0;
        }

        return $distance;
    }

    /**
     * 计算两点距离及预计时间 按照骑行计算
     * @param $s_lat
     * @param $s_lng
     * @param $e_lat
     * @param $e_lng
     * @return mixed
     */
    public function getDistances($origins,$destinations)
    {
        foreach ($origins as &$item){
            $item = implode(',',$item);
        }
        $origins_str = implode('|',$origins);

        foreach ($destinations as &$item){
            $item = implode(',',$item);
        }
        $destinations_str = implode('|',$destinations);

        $str = "http://api.map.baidu.com/routematrix/v2/driving?output=json&origins={$origins_str}&destinations={$destinations_str}&ak=$this->ak";
        $rt = json_decode(file_get_contents($str));

        if($rt->status == 0){
            $rtn = [];
            foreach ($rt->result as $item){
                $distance['distance'] = $item->distance->value ?? 0;
                $distance['remaining_time'] = $item->duration->value ?? 0;
                $rtn[] = $distance;
            }
        }else{
            return false;
        }

        return $rtn;
    }
}